//
//  GetData1.m
//  BaseFrame
//
//  Created by yons on 16/6/13.
//  Copyright © 2016年 xiaojiang. All rights reserved.
//

#import "GetData1.h"
@interface GetData1()<UITableViewDataSource,UITableViewDelegate>
{
    UIScrollView *MainscrollView;
    UILabel *titleLab;
    UIView *container;
    UITableView *_tableView;
    NSArray *dataSource;
}

@end

@implementation GetData1
-(id)initWithTitle:(NSString *)title andArr:(NSArray *)resultArr{
    self = [super init];
    if (self) {
        dataSource = resultArr;
        WS(ws);
        [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
        self.frame = [UIScreen mainScreen].bounds;
        self.backgroundColor = [UIColor colorWithWhite:0.245 alpha:0.400];
        
        MainscrollView = [[UIScrollView alloc] init];
        [self addSubview:MainscrollView];
        MainscrollView.backgroundColor = [UIColor whiteColor];
        MainscrollView.layer.cornerRadius = 8;
        MainscrollView.layer.masksToBounds = YES;
        MainscrollView.bounces = YES;
        MainscrollView.scrollEnabled = YES;
        //scrollview中间的view
        container = [UIView new];
        [self addSubview:container];
        [container mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(ws);
            make.left.mas_equalTo(30);
            make.right.mas_equalTo(-30);
        }];
        container.layer.masksToBounds = YES;
        titleLab = [UILabel new];
        [container addSubview:titleLab];
        titleLab.text = title;
        titleLab.numberOfLines = 0;
        titleLab.textAlignment = NSTextAlignmentCenter;
        [titleLab sizeToFit];
        titleLab.font = FONT(16);
        [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(15);
            make.left.mas_equalTo(15);
            make.right.mas_equalTo(-15);
        }];
        
        _tableView = [[UITableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [container addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(titleLab.mas_bottom).offset(10);
            make.left.mas_equalTo(0);
            make.right.mas_equalTo(0);
            make.height.mas_equalTo(120);
            
        }];
        
        UIButton *cancle = [UIButton new];
        [container addSubview:cancle];
        cancle.backgroundColor = [UIColor whiteColor];
        [cancle setTitle:@"取消" forState:UIControlStateNormal];
        [cancle setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        cancle.layer.cornerRadius = 8;
        [cancle addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
        [cancle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(0);
            make.top.equalTo(_tableView.mas_bottom).offset(10);
            make.height.mas_equalTo(45);
            make.right.mas_equalTo(0);
        }];
        [container mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(cancle.mas_bottom).offset(0);
        }];
        [MainscrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(container).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
        }];
        
    }
    return self;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *cellID = @"cellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.textLabel.text = dataSource[indexPath.row];
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.block) {
        NSString *str = [NSString stringWithFormat:@"%ld",indexPath.row];
        
        self.block(dataSource[indexPath.row],str);
        
    }
}

-(void)show{
    UIWindow *win = [[UIApplication sharedApplication] keyWindow];
    UIView *topView = [win.subviews lastObject];
    [topView addSubview:self];
}

-(void)hide{
    [self removeFromSuperview];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
